package com.maoshi.shop.mp.comment.api.config;

import com.maoshi.shop.mp.comment.api.handler.MenuHandler;
import com.maoshi.shop.mp.comment.api.handler.MsgHandler;
import com.maoshi.shop.mp.comment.api.handler.SubscribeHandler;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信路由的配置
 * @author LGH
 */
@Configuration
@AllArgsConstructor
public class WxMpMessageRouterConfig {

    private final MenuHandler menuHandler;
    private final MsgHandler msgHandler;
    private final SubscribeHandler subscribeHandler;
    private final WxMpService wxMpService;

    @Bean
    public WxMpMessageRouter messageRouter() {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);

        // 自定义菜单事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.CLICK).handler(this.menuHandler).end();

        // 关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE).handler(this.subscribeHandler)
                .end();

        // 默认
        newRouter.rule().async(false).handler(this.msgHandler).end();

        return newRouter;
    }

}
